package com.idanchuang.support.etcache.support.jetcache;

import com.alicp.jetcache.CacheBuilder;
import com.alicp.jetcache.autoconfigure.ConfigTree;
import com.alicp.jetcache.autoconfigure.EmbeddedCacheAutoInit;
import com.idanchuang.support.etcache.EtCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;

/**
 * @author yjy
 */
//@Configuration
@Conditional(EtCacheCondition.class)
public class DoEtCacheJetCacheAutoConfiguration {

    @Bean
    public DoEtCacheJetCacheAutoConfiguration.EtCacheAutoInit etCacheAutoInit(EtCacheManager manager) {
        return new DoEtCacheJetCacheAutoConfiguration.EtCacheAutoInit(manager);
    }

    public static class EtCacheAutoInit extends EmbeddedCacheAutoInit {

        private final EtCacheManager manager;

        public EtCacheAutoInit(EtCacheManager manager) {
            super("etcache");
            this.manager = manager;
        }

        @Override
        protected CacheBuilder initCache(ConfigTree ct, String cacheAreaWithPrefix) {
            return EtCacheJetCacheBuilder.createEtCacheBuilder().manager(manager);
        }
    }


}
